package com.base.study.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.study.R;
import com.base.study.bean.SubjectListBean;
import com.base.study.bean.SubjectResoutEntity;
import com.base.study.databinding.ActivitySubjectBinding;
import com.base.study.presenter.SubjectPresenter;
import com.base.study.view.SubjectView;
import com.base.study.widget.RecyclerViewSpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weikailiang on 2020/3/8.
 */
@Route(path = "/subject/subject")
public class SubjectActivity extends CBaseBarMvpActivity<SubjectPresenter> implements SubjectView{
    private ActivitySubjectBinding mBinding;
    private List<SubjectResoutEntity.RowsBean> mData=new ArrayList<>();
    private BaseAdapter<SubjectResoutEntity.RowsBean> subjectListBeanBaseAdapter;
    @Autowired
    String formId;
    private int page = 1;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_subject;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivitySubjectBinding) mRootBinding;
        mPresenter = new SubjectPresenter(this,this);
        mPresenter.start();
        mBinding.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.smartRefreshLayout.setHeaderHeight(60);
        mBinding.smartRefreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        setBackNavigation();
        setBarTitle("试题");
    }

    @Override
    protected void start() {
        mBinding.smartRefreshLayout.autoRefresh();
        subjectListBeanBaseAdapter = new BaseAdapter<SubjectResoutEntity.RowsBean>(mData, mContext) {
            @Override
            public void convert(BaseHolder holder, final SubjectResoutEntity.RowsBean subjectListBean, boolean isHeader, boolean isBottom) {
                TextView questionsName = holder.getView(R.id.questionsName);
                TextView createDate = holder.getView(R.id.createDate);
                TextView subordinateToQuestion = holder.getView(R.id.subordinateToQuestion);
                TextView questionsType = holder.getView(R.id.questionsType);
                TextView rootSubjectId = holder.getView(R.id.rootSubjectId);
                questionsName.setText(subjectListBean.getQuestionsName());
                createDate.setText(subjectListBean.getCreateDate());
                subordinateToQuestion.setText(subjectListBean.getSubordinateToQuestion());
                questionsType.setText(subjectListBean.getQuestionsType());
                rootSubjectId.setText(subjectListBean.getRootSubjectId());
            }
        };

        subjectListBeanBaseAdapter.putNormalType(R.layout.item_subject);
        subjectListBeanBaseAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                SubjectResoutEntity.RowsBean rows=mData.get(dataPosition);
            }
        });
        mBinding.rvSubjectList.setAdapter(subjectListBeanBaseAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rvSubjectList.setLayoutManager(linearLayoutManager);
        mBinding.rvSubjectList.addItemDecoration(new RecyclerViewSpacesItemDecoration
                (10, 10, 10, 10));

        mBinding.smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                mPresenter.getList(page, 10);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                mPresenter.getList(page, 10);
            }
        });

    }


    @Override
    public void returnNoData() {
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.empty.setVisibility(View.VISIBLE);
    }

    @Override
    public void returnList(SubjectResoutEntity bean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishRefresh();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.clear();
        mData.addAll(bean.getRows());
        subjectListBeanBaseAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnMoreList(SubjectResoutEntity bean) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(false);
        mData.addAll(bean
                .getRows());
        subjectListBeanBaseAdapter.notifyDataSetChanged();

    }

    @Override
    public void returnNoMoreData() {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.smartRefreshLayout.finishLoadmore();
        mBinding.smartRefreshLayout.setLoadmoreFinished(true);
    }

    @Override
    public void returnError() {
        mBinding.empty.setVisibility(View.GONE);
        if (page == 1) {
            mBinding.smartRefreshLayout.finishRefresh();
        } else {
            mBinding.smartRefreshLayout.finishLoadmore();
        }
    }
}
